//
//  Queue.swift
//  DataStructureAndAlgorithm
//
//  Created by icemelon on 2021/8/4.
//

import Foundation

struct Queue<T: Equatable> {
    private var queue = BothWayLinkList<T>()
    
    var count: Int {
        get {
            return queue.count
        }
    }
    
    func isEmpty() -> Bool {
        return queue.count == 0
    }
    
    mutating func enquequ(element: T) {
        queue.add(element: element)
    }
    
    mutating func dequeue() {
        queue.remove(at: 0)
    }
    
    func front() -> T? {//获取队头
        if let node = queue.first {
            return node.element
        }
        return nil
    }
    
    var description: String {
        get {
            var str = "queue -> \(count) \n"
            str.append(queue.description)
            return str
        }
    }
    
}

extension Queue {
    static func testQueue() {
        func testQueue() {
            var queue = Queue<Int>()
            queue.enquequ(element: 1)
            queue.enquequ(element: 2)
            queue.enquequ(element: 3)
            queue.enquequ(element: 4)
            
            queue.dequeue()
            print("队头袁术：", queue.front()?.description)
            print(queue.description)
            
        }
    }
}
